package com.ticket.sass.ticketserver.manager

import com.google.gson.Gson
import com.ticket.sass.common.ticketserver.ApiInterface
import com.ticket.sass.common.ticketserver.TicketServerBuilder
import com.ticket.sass.common.ticketserver.configure.Config
import com.ticket.sass.common.ticketserver.configure.ConfigData
import com.ticket.sass.common.ticketserver.enums.Platform
import com.ticket.sass.common.ticketserver.toJson
import org.springframework.stereotype.Component
import javax.servlet.http.HttpServletRequest


/**
 * @author 朱其鹏
 * @date 2024/3/23 20:44
 */
@Component
class TicketServerManager(
    private val ticketServerBuilder: TicketServerBuilder,
    private val request: HttpServletRequest,
) {
    val api: ApiInterface get() = apiBuild()

    fun apiBuild(): ApiInterface {
        val platform = Platform.numberOf(request.getAttribute("platform").toString().toShort())
        val config = Gson().fromJson(request.getAttribute("config").toString(), platform.configData)

        return ticketServerBuilder.apiBuild(Config(platform, config))
    }
}